
import os
import gtk
import gtk.glade
from BuildWizException import BuildWizException
from DlgDirectorySelection import DlgDirectorySelection

class DlgDeployPen:
    def __init__( self, wizard, platfname):
        self.__wiz = wizard
        self.__values = {}
        self.__entryDefault = { "bg": "#FFFFFF", "fg": "#000000" }
        self.__entryWrong = { "bg": "#F74646", "fg": "#FFFFFF" }
        self.__entryOK = { "bg": "#8DF793", "fg": "#000000" }
        self.__platfname = platfname
        self.__glade = self.__wiz.getPreferences().getGUIFile( "deploypenwindow")
        if not os.path.isfile( self.__glade):
            raise BuildWizException( "DlgDeployPen: libglade could not open - [%s]" % self.__glade)
        self.__xml = gtk.glade.XML( self.__glade)
        if self.__xml:
            self.__win = self.__xml.get_widget( "DlgDeployPen")
            self.__win.connect( "destroy", self.__onCancel)
            self.__handlers = { "on_entryInstallDir_changed": self.__onInstallDir
                              , "on_btnBrowseDeployDir_clicked": self.__onBrowseInstallDir
                              , "on_entryDestDir_changed": self.__onDestDir
                              , "on_entryDirNameFormation_changed": self.__onDirNameFormation
                              , "on_cmbboxDateTimeFormat_changed": self.__onDateTimeFormat
                              , "on_entryLinkName_changed": self.__onLinkName
                              , "on_btnCancel_clicked": self.__onCancel
                              , "on_btnOK_clicked": self.__onOK 
                              , "on_chkbtnUmount_toggled": self.__onUmount 
                              , "on_chkbtnDeployAfterBuildProcess_toggled": self.__onDeployAfterBuild }
            self.__xml.signal_autoconnect( self.__handlers)
            self.__btnOK = self.__xml.get_widget( "btnOK")
            self.__btnOK.set_sensitive( False)
            self.__btnCancel = self.__xml.get_widget( "btnCancel")
            self.__entryInstallDir = self.__xml.get_widget( "entryInstallDir")
            self.__entryDestDir = self.__xml.get_widget( "entryDestDir")
            self.__entryDirNameFormation = self.__xml.get_widget( "entryDirNameFormation")
            self.__cmbboxDateTime = self.__xml.get_widget( "cmbboxDateTimeFormat")
            self.__cmbboxDateTimeModel = self.__cmbboxDateTime.get_model()
            self.__entryLinkName = self.__xml.get_widget( "entryLinkName")
            self.__chkbtnUmount = self.__xml.get_widget( "chkbtnUmount")
            self.__chkbtnDeployAfterBuildProcess = self.__xml.get_widget( "chkbtnDeployAfterBuildProcess")
            self.__isInitialLoading = True
            self.__loadInitialValues()
            self.__isInitialLoading = False

    def __loadInitialValues( self):
        values = self.__wiz.getDeployPenValues( self.__platfname)
        if len( values) > 0:
            if values.has_key( "source_dir"):
                sourcedir = values[ "source_dir"]
                if sourcedir == "":
                    sourcedir = self.__wiz.getPlatformProjectDir( self.__platfname)
                self.__entryInstallDir.set_text( sourcedir)

            if values.has_key( "destiny_dir"):
                destdir = values[ "destiny_dir"]
                if destdir == "":
                    destdir = "/misc"
                self.__entryDestDir.set_text( destdir)

            if values.has_key( "dir_name"):
                dirname = values[ "dir_name"]
                if dirname == "":
                    dirname = self.__platfname
                self.__entryDirNameFormation.set_text( dirname)

            if values.has_key( "dir_name_datetime"):
                dt = values[ "dir_name_datetime"]
                if dt == "DDMMYYYYHHMMSS":
                    self.__cmbboxDateTime.set_active( 1)
                else:
                    self.__cmbboxDateTime.set_active( 0)

            if values.has_key( "link_2_dest_dir"):
                linkname = values[ "link_2_dest_dir"]
                if linkname == "":
                    linkname = "LINK_2_BINS"
                self.__entryLinkName.set_text( linkname)

            if values.has_key( "umount_after_deploy"):
                self.__chkbtnUmount.set_active( values[ "umount_after_deploy"])

            if values.has_key( "deploy_after_build_process"):
                self.__chkbtnDeployAfterBuildProcess.set_active( values[ "deploy_after_build_process"])

    def __try2EnableOK( self):
        self.__values.clear()
        self.__values[ "source_dir"] = self.__entryInstallDir.get_text()
        self.__values[ "destiny_dir"] = self.__entryDestDir.get_text()
        self.__values[ "dir_name"] = self.__entryDirNameFormation.get_text()
        index = self.__cmbboxDateTime.get_active()
        self.__values[ "dir_name_datetime"] = self.__cmbboxDateTimeModel[ index][ 0]

        self.__values[ "link_2_dest_dir"] = self.__entryLinkName.get_text()
        self.__values[ "umount_after_deploy"] = self.__chkbtnUmount.get_active()
        self.__values[ "deploy_after_build_process"] = self.__chkbtnDeployAfterBuildProcess.get_active()

        if os.path.isdir( self.__entryInstallDir.get_text()) and \
            len( self.__entryDestDir.get_text()) > 0 and \
            len( self.__entryDirNameFormation.get_text()) > 0 and \
            len( self.__entryLinkName.get_text()) > 0:
            self.__btnOK.set_sensitive( True)
        else:
            self.__btnOK.set_sensitive( False)


    def run( self):
        return self.__win.run()

    def getDeployValues( self):
        return self.__values

    # FIXME: Duplicated code, see class DlgNewPlatform
    def __setEntryColorStatus( self, entry, status):
        # This does not yield expected results. 
        entry.modify_bg( gtk.STATE_NORMAL, gtk.gdk.color_parse( status[ "bg"]))
        entry.modify_fg( gtk.STATE_NORMAL, gtk.gdk.color_parse( status[ "fg"]))

    def __onInstallDir( self, entry):
        self.__values[ "source_dir"] = entry.get_text()
        if len( self.__values[ "source_dir"]) == 0:
            self.__setEntryColorStatus( entry, self.__entryDefault)
        else:
            if os.path.isdir( self.__values[ "source_dir"]):
                self.__setEntryColorStatus( entry, self.__entryOK)
            else:
                self.__setEntryColorStatus( entry, self.__entryWrong)

        if not self.__isInitialLoading:
            self.__try2EnableOK()

    def __onBrowseInstallDir( self, btn):
        fs = DlgDirectorySelection( "Choose the project diretory")
        selecteddir = fs.getDirSelected()
        if len( selecteddir) > 0:
            self.__entryInstallDir.set_text( selecteddir)

        self.__values[ "destiny_dir"] = selecteddir
        self.__try2EnableOK()

    def __onDestDir( self, entry):
        if not self.__isInitialLoading:
            self.__values[ "destiny_dir"] = entry.get_text()
            self.__try2EnableOK()

    def __onDirNameFormation( self, entry):
        if not self.__isInitialLoading:
            self.__values[ "dir_name"] = entry.get_text()
            self.__try2EnableOK()

    def __onDateTimeFormat( self, combo):
        if not self.__isInitialLoading:
            index = combo.get_active()
            self.__values[ "dir_name_datetime"] = self.__cmbboxDateTimeModel[ index][ 0]
            self.__try2EnableOK()

    def __onLinkName( self, entry):
        if not self.__isInitialLoading:
            self.__values[ "link_2_dest_dir"] = entry.get_text()
            self.__try2EnableOK()

    def __onCancel( self, btn):
        self.__win.destroy()

    def __onOK( self, btn):
        self.__win.destroy()

    def __onUmount( self, chkbtn):
        if not self.__isInitialLoading:
            self.__values[ "umount_after_deploy"] = chkbtn.get_active()
            self.__try2EnableOK()

    def __onDeployAfterBuild( self, chkbtn):
        if not self.__isInitialLoading:
            self.__values[ "deploy_after_build_process"] = chkbtn.get_active()
            self.__try2EnableOK()

